package com.jack.online.study.controller.edu.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jack.online.study.common.base.result.R;
import com.jack.online.study.model.edu.dao.Teacher;
import com.jack.online.study.model.edu.vo.TeacherQueryVo;
import com.jack.online.study.service.edu.TeacherService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author jack
 * @since 2024-04-17
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/admin/edu/teacher")
public class TeacherController {

    @Resource
    private TeacherService teacherService;


    @GetMapping("list")
    public R listAll() {
        List<Teacher> list = teacherService.list();
        return R.ok().data("items", list).message("获取讲师列表成功");
    }

    /**
     * 根据id逻辑删除讲师
     */
    @DeleteMapping("remove/{id}")
    public R removeById(@PathVariable String id) {
        //删除图片
        teacherService.removeAvatarById(id);
        //删除讲师
        boolean result = teacherService.removeById(id);
        if (result) {
            return R.ok().message("删除成功");
        } else {
            return R.error().message("数据不存在");
        }
    }

    /**
     * 分页讲师列表
     */
    @GetMapping("list/{page}/{limit}")
    public R listPage(@PathVariable Long page,
                      @PathVariable Long limit, TeacherQueryVo teacherQueryVo) {

        Page<Teacher> pageParam = new Page<>(page, limit);
        IPage<Teacher> pageModel = teacherService.selectPage(pageParam, teacherQueryVo);
        List<Teacher> records = pageModel.getRecords();
        long total = pageModel.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }

    /**
     *新增讲师
     */
    @PostMapping("save")
    public R save(@RequestBody Teacher teacher) {
        boolean result = teacherService.save(teacher);
        if (result) {
            return R.ok().message("保存成功");
        } else {
            return R.error().message("保存失败");
        }
    }

    /**
     * 更新讲师
     */
    @PutMapping("update")
    public R updateById(@RequestBody Teacher teacher) {
        boolean result = teacherService.updateById(teacher);
        if (result) {
            return R.ok().message("修改成功");
        } else {
            return R.error().message("数据不存在");
        }
    }

    /**
     *根据id获取讲师信息
     */
    @GetMapping("get/{id}")
    public R getById(@PathVariable String id) {
        Teacher teacher = teacherService.getById(id);
        if (teacher != null) {
            return R.ok().data("item", teacher);
        } else {
            return R.error().message("数据不存在");
        }
    }

    /**
     * 根据id列表删除讲师
     */
    @DeleteMapping("batch-remove")
    public R removeRows(@RequestBody List<String> idList) {
        boolean result = teacherService.removeByIds(idList);
        if (result) {
            return R.ok().message("删除成功");
        } else {
            return R.error().message("数据不存在");
        }
    }

    /**
     *根据左关键字查询讲师名列表
     */
    @GetMapping("list/name/{key}")
    public R selectNameListByKey(@PathVariable String key) {

        List<Map<String, Object>> nameList = teacherService.selectNameListByKey(key);

        return R.ok().data("nameList", nameList);
    }

    /**
     * 测试服务调用
     */
    @GetMapping("test")
    public R test() {
//        ossFileService.test();
        log.info("edu执行成功");
        return R.ok();
    }

    /**
     * 测试并发
     */
    @GetMapping("test_concurrent")
    public R testConcurrent() {
        log.info("test_concurrent");
        return R.ok();
    }

}
